package com.example.yls.damoa;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btnQuery;
    private TextView txtResult;
    private EditText edtNo;
    private EditText edtName;
    private EditText edtAge;
    private Button btnAdd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtResult=findViewById(R.id.txtResult);
        btnQuery=findViewById(R.id.btn_query);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                Cursor cursor=getContentResolver().query(uri,null,null,null,null,null);
                if (cursor!=null&&cursor.getCount()>0){
                    StringBuffer buffer=new StringBuffer();
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno=cursor.getString(0);
                        String name=cursor.getString(1);
                        int age=cursor.getInt(2);
                        buffer.append(stuno).append(" ").append(name).append(" ").append(age).append("\n");
                    }
                    txtResult.setText(buffer);
                }else{
                    txtResult.setText("行数为0");
                }
            }
        });
        edtAge=findViewById(R.id.edtAge);
        edtName=findViewById(R.id.edtName);
        edtNo=findViewById(R.id.edtNo);
        btnAdd=findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no=edtNo.getText().toString();
                String name=edtName.getText().toString();
                int age= Integer.parseInt(edtAge.getText().toString());
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                ContentValues values=new ContentValues();
                values.put("stuno",no);
                values.put("name",name);
                values.put("age",age);
                getContentResolver().insert(uri,values);
            }
        });
    }
}
